<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-upload"
      @click="dialogFormVisible = true"
    >
      导入Excel
    </el-button>
    <el-button type="primary" icon="el-icon-download" @click="exportData">
      导出Excel
    </el-button>

    <el-dialog title="数据字典导入" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item>
          <el-upload
            :action="`${BASE_API}/admin/core/dict/import`"
            :auto-upload="true"
            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            :multiple="false"
            :limit="1"
            name="file"
            :on-success="handlerSuccess"
            :on-error="handlerError"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <el-table
      border
      row-key="id"
      lazy
      :load="loadDict"
      style="margin-top:20px"
      
      :data="list">
       <el-table-column
        prop="name"
        label="名称"
       
      ></el-table-column>
      <el-table-column
        prop="dictCode"
        label="编码"
        
      ></el-table-column>
      <el-table-column prop="value" label="属性值" ></el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      dialogFormVisible: false,
      BASE_API: process.env.VUE_APP_BASE_API,
    }
  },
  methods: {
    handlerSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.$message.success('数据导入成功')
      } else {
        this.$message.error('数据导入失败')
      }
    },
    handlerError(err, file, fileList) {
      this.$message.error('数据导入失败')
    },
    //导出数据
    exportData() {
      window.location.href = this.BASE_API + '/admin/core/dict/export '
    },
    //根据父id查询数据
    async getByparentId(id) {
      const result = await this.$API.dict.getListByParentId(id)
      if (result.code == 200) {
        this.list = result.data.list
      }
    },
    async loadDict(tree,treeNode,reslove){
     const result = await this.$API.dict.getListByParentId(tree.id);
     if(result.code==200){
         reslove(result.data.list)
     }
     
    }
  },
  mounted() {
    this.getByparentId(1)
  },
}
</script>

<style></style>
